package com.iory.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TianwangDbService {
	private Context context;
	private  TianwangSqliteHelper sqliteHelper = null;
	
	public TianwangDbService(Context context){
		this.context = context;
		sqliteHelper = new TianwangSqliteHelper(context);
	}
	
	public void insert(String name , String rtsp , String province,String city){
		SQLiteDatabase  db = sqliteHelper.getWritableDatabase();
		try {
			db.beginTransaction();
			
				ContentValues values = new ContentValues();
				
				// will not insert the same song
						
							values.put("_name", name);
							values.put("_rtsp", rtsp);
							values.put("_province", province);
							values.put("_city", city);
							//long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
							db.insert(TianwangSqliteHelper.TABLE_CAMERA, "", values);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.setTransactionSuccessful();
			db.endTransaction();
			if(db!=null){
				db.close();
			}
		}
	}
	
	public void insert(CameraDateItem cameraItem){
		SQLiteDatabase  db = sqliteHelper.getWritableDatabase();
		try {
			db.beginTransaction();
			
				ContentValues values = new ContentValues();
				
				// will not insert the same song
						
							values.put("_name", cameraItem.name);
							values.put("_rtsp", cameraItem.rtsp);
							values.put("_province", cameraItem.province);
							values.put("_city", cameraItem.city);
							//long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
							db.insert(TianwangSqliteHelper.TABLE_CAMERA, "", values);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.setTransactionSuccessful();
			db.endTransaction();
			if(db!=null){
				db.close();
			}
		}
	}
	
	public void deleteAll(){
		SQLiteDatabase  db = sqliteHelper.getWritableDatabase();
		try {
			db.beginTransaction();
			String delSql = "DELETE FROM  "+ TianwangSqliteHelper.TABLE_CAMERA+" WHERE _id > 0";
			db.execSQL(delSql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.setTransactionSuccessful();
			db.endTransaction();
			if(db!=null){
				db.close();
			}
		}
	}
	
	public List<CameraDateItem> querySongs(int count){
		List<CameraDateItem> cameraItems = new ArrayList<CameraDateItem>();
		SQLiteDatabase  db = sqliteHelper.getReadableDatabase();
		String querySql = "select _id, _name, _rtsp, _province, _city, _free  from cameraList  order by _id asc";
		if (count > 0)
			querySql = querySql + " limit " + count;
		Cursor cursor = null;
		CameraDateItem cameraItem  = null;
		try {
			cursor = db.rawQuery(querySql, null);
			while(cursor.moveToNext()){
				cameraItem = new CameraDateItem();
				cameraItem.id = cursor.getString(cursor.getColumnIndex("_id")) ;
				cameraItem.name = cursor.getString(cursor.getColumnIndex("_name"));
				cameraItem.rtsp 	   = cursor.getString(cursor.getColumnIndex("_rtsp"));
				cameraItem.province    = cursor.getString(cursor.getColumnIndex("_province"));
				cameraItem.city      = cursor.getString(cursor.getColumnIndex("_city"));
				cameraItem.free = cursor.getString(cursor.getColumnIndex("_free"));
				cameraItems.add(cameraItem);
			}			
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null)
				cursor.close();
			db.close();
		}
		
		return cameraItems;
	}
	
}
